# coding = utf-8
from selenium import webdriver  # 导入火狐浏览器的驱动
import time
import datetime

webdriver = webdriver.Firefox()     # 打开一个火狐浏览器
webdriver.maximize_window()


def buy_on_time(buytime):
    while True:
        now = datetime.datetime.now()
        if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:
            for i in range(1, 21):#每隔0.2秒抢购一次，尝试抢购20次
				webdriver.find_element_by_class_name("submit-btn")
                webdriver.find_element_by_id("order-submit").click()
                print(now.strftime('%Y-%m-%d %H:%M:%S'))
                print("第%d次抢购" % i)
                time.sleep(0.2)
            time.sleep(3)
            print('purchase success')
        time.sleep(0.5)
webdriver.get("https://cart.jd.com/cart.action")  # 此为购物车地址 https://cart.jd.com/cart.action
time.sleep(3) 
webdriver.find_element_by_class_name("login-btn").click() # 一般需要登录，此处点击的是去登录按钮 
time.sleep(50)  # 为了避免输入校验码绕过了输入登录账户密码的步骤，此处打开的是二维码页面，请在50秒内用手机app扫描登录。 
buy_on_time("2020-05-22 00:00:00")#开始抢购时间
